<?php
if (@$_POST['open'] != 'please') {
    exit;
}
include_once('../../php/include_all.php');
?>
 
   
        <div class="widget-header widget-header-flat">
            <h5><i class="icon-home"></i>  SKPD</h5>
        </div>
        <div class="position-relative" id="page-content">
            <a href="#modalwin" data-toggle="modal" class="btn btn-small btn-primary2 no-radius btn-tambah"><i class="icon-plus"></i>Tambah Data</a>
			 &nbsp;<span id="load" class="spinner"></span>
	   </div>
        <div id="modalwin" class="modal hide fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

            <header class="modal-header">
                <a href="#" class="close" data-dismiss="modal">x</a>
                <h3>Data SKPD</h3>
            </header>

            <div class="modal-body">
                <form id="formSKPD">
                    <input type="hidden" name="act" value="skpd_simpan">
                    <input type="hidden" id="id_skpd" name="id_skpd" value="0"> 
                    <table class="table-form">
                        <tbody>
                            <tr>                            
                                <td>Sub Bidang</td>
                                <td>:</td>
                                <td>
                                    <select name="id_subbidang"><option value="0" id='id_sub'>-pilih sub bidang- </option>
                                        <?php
                                        $bidang = get_datas("select * from mus_subbidangbappeda order by id_subbidangbappeda");
                                        foreach ($bidang as $bidang) {
                                            ?>
                                            <option value="<?php echo $bidang['id_subbidangbappeda']; ?>"><?php echo $bidang['nama_subbidang']; ?>
                                                <?php
                                            }
                                            ?>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td valign="middle">Klasifikasi</td>
                                <td valign="middle">:</td>
                                <td>
                                    <select name="id_klasifikasi" id="id_klasifikasi" ><option value="0" id='id_klasi'>-pilih klasifikasi-</option>
                                        <?php
                                        $urusan = get_datas("select * from mus_klasifikasi order by id_klasifikasi");
                                        foreach ($urusan as $urusan) {
                                            ?>
                                            <option value="<?php echo $urusan['id_klasifikasi']; ?>"><?php echo $urusan['nama_klasifikasi']; ?></option>
                                            <?php
                                        }
                                        ?>
                                    </select>
                                </td>
                            </tr>

                            <tr>
                                <td>Kode SKPD</td>
                                <td>:</td>
                                <td>
                                    <input type="text" name="kode_skpd" id="kode_skpd"  readonly/>
                                </td>
                            </tr>

                            <tr>
                                <td>Nama SKPD Skpd</td>
                                <td>:</td>
                                <td>
                                    <input type="text" name="nama_skpd" id="nama_skpd"  />
                                </td>
                            </tr>
                            <tr>
                                <td></td>
                                <td></td>
                                <td>
                                    <a class="btn btn-primary2 btn-small btn-simpan-skpd"><div id="loader"></div><i class="icon-save"></i>&nbsp;Simpan</a><span id="result"></span>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </form>
            </div>
        </div>
       
        <div class="box-content">
            <table class="table table-bordered geo-table table-hover" width="100%">
                <thead>
                    <tr>
                        
                        <td width="12%" class="center">Kode Klasifikasi/  Kode SKPD</td> 
                        <td width="33%" class="center">Klasifikasi/Nama SKPD</td>
                        <td width="32%" class="center">Nama Subbidang</td>
                        <td width="20%" class="center">Aksi</td>
                    </tr>
                </thead>
                <tbody id="tampilSKPD">
                    <?php
                    $no = 1;
                    $kla = exec_query("select * from mus_klasifikasi order by id_klasifikasi");
                    while ($rw1 = pg_fetch_array($kla)) {
                        $id1 = $rw1['id_klasifikasi'];
                        ?><tr>
                            
                            <td><?php echo $id1 ?></td>
                            <td><b><?php echo $rw1['nama_klasifikasi'] ?></b></td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                        </tr>
                        <?php
                        $skpd = exec_query("select id_skpd, kode_skpd, nama_skpd, nama_subbidang from mus_skpd s, mus_subbidangbappeda b where id_klasifikasi = " . $rw1['id_klasifikasi'] . " AND s.id_subbidangbappeda=b.id_subbidangbappeda order by id_skpd");
                        while ($rw2 = pg_fetch_array($skpd)) {
                            ?><tr>
                              
                                <td><?php echo $rw2['kode_skpd'] ?></td>
                                <td><?php echo $rw2['nama_skpd'] ?></td>
                                <td><?php echo $rw2['nama_subbidang'] ?></td>
                                <td>
                                    <a href="#modalwin" data-toggle="modal" class="btn btn-info btn-small bt-edit"  name="<?php echo $rw2['id_skpd'] ?>"><i class="icon-edit icon-white"></i>Ubah</a>
                                 	<a class='btn btn-danger btn-small bt-hapus' name="<?php echo $rw2['id_skpd'] ?>"><i class="icon-trash icon-white"></i>Hapus</a>   
                                </td>
                            </tr>
                            <?php
                            $no++; 
                        }

                        //$no++;
                    }
                    ?>
                </tbody>
            </table>    
        </div>
  
 
<script>
    var url = 'admin/master_skpd/skpd_save.php';
    
    $('#id_klasifikasi').change(function(){
        source ="admin/master_skpd/id.php?idkla="+this.value;
        var coba=$.get(source);
        coba.done(function(data){          
            document.getElementById('kode_skpd').value=data;	
        })
    });
    
    $('.btn-simpan-skpd').click(function(){      
        var btn = $(this);
        var load = $('#loader');
        var rslt = $('#result');
		
        load.addClass('icon-spinner icon-spin icon-2x white');
       // btn.removeClass('btn-primary2').addClass('btn-info');
        
        var form = $('#formSKPD');
        var data = form.serializeArray();
        var post = $.post(url,data);
        post.done(function(res){
            var result = res.split('__');
            if(result.length==3){
                if(result[0]=='success'){
                    rslt.html('<font color="green">Data Tersimpan...</font>');
			//		btn.removeClass('btn-info').addClass('btn-primary2');
                    setTimeout(function(){
                        rslt.html('');
                    }, 1500);
                
                    var tbody = $('#tampilSKPD');
                    tbody.html(result[2]);
                    init();
					$("#id_skpd").val("0");
                    document.getElementById("id_sub").selected = true;
                    document.getElementById("id_klasi").selected = true;
                    $("#kode_skpd").val("");
                    $("#nama_skpd").val("");
					$('#modalwin').modal('hide');
                }else{
                    rslt.html('<font color="red">'+res+'</font>');
                }
            }else{
                rslt.html('<font color="red">'+res+'</font>');
            }
            load.removeClass();
            btn.removeClass('.btn-info').addClass('.btn-primary2');
        });
    
    });
    
	$('.btn-tambah').click(function(){
		$("#id_skpd").val("0");
        document.getElementById("id_sub").selected = true;
        document.getElementById("id_klasi").selected = true;
        $("#kode_skpd").val("");
        $("#nama_skpd").val("");
	});
	
	
    function init(){
        $('.bt-edit').click(function(){
            var idskpd = this.name;
            var form = $('#formSKPD');
            var post = $.post(url,{act:'ubah_skpd',id_skpd:idskpd});            
            post.done(function(res){
                var value = res.split('__');
                $('#id_skpd').val(value[0]);
                form.find('select[name="id_subbidang"]').val(value[1]);
                form.find('select[name="id_klasifikasi"]').val(value[2]);
                form.find('input[name="kode_skpd"]').val(value[3]);
                form.find('input[name="nama_skpd"]').val(value[4]);
            });
        });
        
        $('.bt-hapus').click(function(){
            var btn = $(this);
            var pid = this.name;
            var load = $(this);
            load.html('<div class="icon-spinner icon-spin icon-2x white"></div>');
            bootbox.confirm("Anda akan menghapus bidang ini?", function(hsl){
                if(hsl==true){
                    if(pid!=0){
                        var post = $.post(url,{act:"hapus_skpd",id_skpd:pid});
                        post.done(function(res){
                            var hasil = res.split('__');
                            //alert(hasil.length);
                            if(hasil.length == 2){
                                if(hasil[0] == 'success'){
                                    rslt.html('<font color="green">Data Telah Dihapus...</font>');
                                    setTimeout(function(){
                                        rslt.html('');
                                    }, 1500);
                                    var tbody = $('#tampilSKPD');
                                    tbody.html(hasil[1]);
                                    init();
                                    //btn.parent().parent().remove();
                                }
                            }
                        });
                        load.html('<i class="icon-trash icon-white"></i> hapus</a>');
                    }
                }else{
                    load.html('<i class="icon-trash icon-white"></i> hapus</a>');
                }
            });
        });
        
    }
	/*
$('.close').click(function(){
	$('#id_skpd').val(0);
	document.getElementById('formSKPD').reset();
});*/
    
	
    init();
    
</script>